package c2.cross.business.championrace;

import cate.game.pvp.championrace.data.ChampionRaceBetRecord;
import cate.game.pvp.championrace.data.ChampionRaceSpec;
import org.springframework.data.annotation.Transient;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;

public class ChampionRaceBetData {
    private HashMap<String, ChampionRaceBetRecord> betData;
    @Transient
    private final Object betLock = new Object();

    public void initialize() {
        if (betData == null) {
            betData = new HashMap<>();
        }
    }

    public ChampionRaceBetRecord getBetRecord(ChampionRaceSpec spec) {
        ChampionRaceBetRecord record;
        synchronized (betLock) {
            record = betData.get(spec.toString());
        }
        return record;
    }

    public List<ChampionRaceBetRecord> getAllBetRecord() {
        List<ChampionRaceBetRecord> record;
        synchronized (betLock) {
            record = new ArrayList<>(betData.values());
        }
        return record;
    }

    public void bet(ChampionRaceSpec spec, byte side, long coins) {
        synchronized (betLock) {
            ChampionRaceBetRecord record = getBetRecord(spec);
            if (Objects.nonNull(record)) {
                record.update(side, coins);
            }
        }
    }

    public void addBetRecord(ChampionRaceBetRecord betRecord) {
        synchronized (betLock) {
            betData.put(betRecord.getSpec().toString(), betRecord);
        }
    }

    public void clear() {
        synchronized (betLock) {
            betData.clear();
        }
    }
}
